<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015-6-17
 * Time: 14:28
 */

namespace doumiaoduo\aliyun\actions;


use doumiaoduo\aliyun\OssuploaderHelper;
use yii\base\Action;
use yii\web\Response;

class OssuploadimageforwechatAction extends Action{

    private $_config;
    public $media_id;

    public function init(){
        $this->_config=\Yii::$app->params['aliyunoss'];
        parent::init();
    }



    public function run()
    {
        if (\Yii::$app->request->isAjax) {
            if (\Yii::$app->request->post('ajax') == 'wechatimage') {
                \Yii::$app->response->format = Response::FORMAT_JSON;
                $this->media_id = \Yii::$app->request->post('media_id');
                $config = array(
                    "pathFormat" => $this->_config['imagePathFormat'],
                    "maxSize" => $this->_config['imageMaxSize'],
                    "allowFiles" => $this->_config['imageAllowFiles'],
                    "media_id" => isset($this->media_id) && !empty($this->media_id) ? $this->media_id : 0,
                );
                if (!empty($this->media_id)) {
                    $up = new OssuploaderHelper('', $config, 'wechat');
                    return $up->getOssFileInfo();
                } else {
                    $data = ["state" => 'FAIL'];
                    return $data;
                }
            }
        }
    }

}